load("@py3_deps//:requirements.bzl", "requirement")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "nodejs_test")

package(default_visibility = ["//visibility:public"])

py_test(
    name = "coalesce_test",
    size = "small",
    srcs = [
        "coalesce.py",
        "coalesce_test.py",
    ],
    python_version = "PY3",
)

sh_test(
    name = "verify-pylint",
    srcs = ["verify-pylint.sh"],
    data = [
        ":pylint_bin",
        "//:all-srcs",
    ],
    tags = ["lint"],
)

nodejs_test(
    name = "verify-tslint",
    data = [
        "//:all-srcs",
        "@npm//tslint",
    ],
    entry_point = "@npm//:node_modules/tslint/bin/tslint",
    tags = ["lint"],
    templated_args = [
        "-p",
        ".",
    ],
)

nodejs_binary(
    name = "update-tslint",
    data = [
        "//:all-srcs",
        "@npm//tslint",
    ],
    entry_point = "@npm//:node_modules/tslint/bin/tslint",
    tags = ["lint"],
    templated_args = [
        "-p",
        ".",
        "--fix",
    ],
)

MISSPELL = "@com_github_client9_misspell//cmd/misspell"

sh_test(
    name = "verify-codegen",
    srcs = ["verify-codegen.sh"],
    args = [
        "$(location :update-codegen)",
        "$(location @io_k8s_code_generator//cmd/client-gen)",
        "$(location @io_k8s_code_generator//cmd/deepcopy-gen)",
        "$(location @io_k8s_code_generator//cmd/informer-gen)",
        "$(location @io_k8s_code_generator//cmd/lister-gen)",
        "$(location @com_github_go_bindata_go_bindata_v3//go-bindata)",
    ],
    data = [
        ":boilerplate/boilerplate.generated.go.txt",  # TODO(fejta): use repo-infra version
        ":update-codegen",
        "//:go.mod",
        "//:go.sum",
        "@//:all-srcs",
        "@com_github_go_bindata_go_bindata_v3//go-bindata",
        "@go_sdk//:files",
        "@io_k8s_code_generator//cmd/client-gen",
        "@io_k8s_code_generator//cmd/deepcopy-gen",
        "@io_k8s_code_generator//cmd/informer-gen",
        "@io_k8s_code_generator//cmd/lister-gen",
    ],
    tags = ["lint"],
)

sh_binary(
    name = "update-codegen",
    srcs = ["update-codegen.sh"],
    args = [
        "$(location @io_k8s_code_generator//cmd/client-gen)",
        "$(location @io_k8s_code_generator//cmd/deepcopy-gen)",
        "$(location @io_k8s_code_generator//cmd/informer-gen)",
        "$(location @io_k8s_code_generator//cmd/lister-gen)",
        "$(location @com_github_go_bindata_go_bindata_v3//go-bindata)",
    ],
    data = [
        ":boilerplate/boilerplate.generated.go.txt",  # TODO(fejta): use repo-infra version
        "//:go.mod",
        "//:go.sum",
        "@com_github_go_bindata_go_bindata_v3//go-bindata",
        "@go_sdk//:files",
        "@io_k8s_code_generator//cmd/client-gen",
        "@io_k8s_code_generator//cmd/deepcopy-gen",
        "@io_k8s_code_generator//cmd/informer-gen",
        "@io_k8s_code_generator//cmd/lister-gen",
    ],
)

sh_test(
    name = "verify-spelling",
    srcs = ["verify-spelling.sh"],
    args = [
        "$(location %s)" % MISSPELL,
        "--error",
    ],
    data = [
        "@//:all-srcs",
        MISSPELL,
    ],
    tags = ["lint"],
)

sh_binary(
    name = "update-spelling",
    srcs = ["update-spelling.sh"],
    args = [
        "$(location %s)" % MISSPELL,
        "-w",
    ],
    data = [
        "@//:all-srcs",
        MISSPELL,
    ],
    tags = ["lint"],
)

protoc = "@com_google_protobuf//:protoc"

protoc_gen_go = "@com_github_golang_protobuf//protoc-gen-go"

protoc_grpc = "plugins=grpc"

# Use the protoc go_out plugin parameter to map the proto import path to the
# Go package import path. Each mapping entry is prefixed with the keyword "M".
# Reference the https://github.com/golang/protobuf "Parameters" section.
proto_importmap = ",".join([
    "Mtestgrid/config/config.proto=k8s.io/test-infra/testgrid/config",
    "Mtestgrid/summary/summary.proto=k8s.io/test-infra/testgrid/summary",
    "Mtestgrid/cmd/summarizer/response/types.proto=k8s.io/test-infra/testgrid/cmd/summarizer/response",
])

sh_binary(
    name = "update-protos",
    srcs = ["update-protos.sh"],
    args = [
        "$(location %s)" % protoc,
        "$(location %s)" % protoc_gen_go,
        "$(location %s)" % ":boilerplate/boilerplate.generated.go.txt",  # TODO(fejta): use repo-infra version
        protoc_grpc,
        proto_importmap,
    ],
    data = [
        ":boilerplate/boilerplate.generated.go.txt",  # TODO(fejta): use repo-infra version
        "//:all-srcs",
        protoc,
        protoc_gen_go,
    ],
    tags = ["local"],
)

sh_test(
    name = "verify-protos",
    srcs = ["verify-protos.sh"],
    args = [
        "$(location %s)" % ":update-protos",
        "$(location %s)" % protoc,
        "$(location %s)" % protoc_gen_go,
        "$(location %s)" % ":boilerplate/boilerplate.generated.go.txt",  # TODO(fejta): use repo-infra version
        protoc_grpc,
        proto_importmap,
    ],
    data = [
        ":boilerplate/boilerplate.generated.go.txt",  # TODO(fejta): use repo-infra version
        ":update-protos",
        protoc,
        protoc_gen_go,
        "@//:all-srcs",
    ],
)

sh_binary(
    name = "update-labels",
    srcs = ["update-labels.sh"],
    args = ["$(location //label_sync)"],
    data = ["//label_sync"],
)

sh_test(
    name = "verify-labels",
    srcs = ["verify-labels.sh"],
    args = [
        "$(location :update-labels)",
        "$(location //label_sync)",
    ],
    data = [
        ":update-labels",
        "//label_sync",
        "//label_sync:labels.md",
        "//label_sync:labels.md.tmpl",
        "//label_sync:labels.yaml",
    ],
    tags = ["lint"],
)

test_suite(
    name = "verify-all",
    tests = [
        ":verify-linters",
        # "@io_k8s_repo_infra//hack:verify-all", # TODO(fejta): golangci-lint
        "@io_k8s_repo_infra//hack:verify-bazel",
        "@io_k8s_repo_infra//hack:verify-boilerplate",
        "@io_k8s_repo_infra//hack:verify-deps",
        "@io_k8s_repo_infra//hack:verify-gofmt",
    ],
)

test_suite(
    name = "verify-linters",
    tags = ["lint"],  # picks up all non-manual targets with this tag
)

py_binary(
    name = "pylint_bin",
    srcs = ["pylint_bin.py"],
    python_version = "PY3",
    tags = ["lint"],
    # NOTE: this should only contain direct third party imports and pylint
    deps = [
        requirement("astroid"),
        requirement("backports.functools_lru_cache"),
        requirement("configparser"),
        requirement("influxdb"),
        requirement("isort"),
        requirement("lazy-object-proxy"),
        requirement("parameterized"),
        requirement("pylint"),
        requirement("pytz"),
        requirement("PyYAML"),
        requirement("requests"),
        requirement("setuptools"),
        requirement("sh"),
        requirement("singledispatch"),
        requirement("six"),
        requirement("wrapt"),
        requirement("ruamel.yaml"),
    ],
)

sh_binary(
    name = "update-gopherage",
    srcs = ["update-gopherage.sh"],
    data = [
        "//gopherage/cmd/html/static:browser_bundle.es2015.js",
    ],
)

sh_test(
    name = "verify-gopherage",
    srcs = ["verify-gopherage.sh"],
    data = [
        "//gopherage/cmd/html/static:browser_bundle.es2015.js",
    ],
    tags = ["lint"],
)

filegroup(
    name = "package-srcs",
    srcs = glob(["**"]),
    tags = ["automanaged"],
    visibility = ["//visibility:private"],
)

filegroup(
    name = "all-srcs",
    srcs = [":package-srcs"],
    tags = ["automanaged"],
    visibility = ["//visibility:public"],
)
